package com.tdr.myandroid.model;

import java.util.ArrayList;

/**
 * Created by lemingdeng on 2014-07-13.
 */
public class EventObject implements EventProtocol {
    public final static int EVENT_TYPE_TIME = 0;
    private ArrayList<ActionObject> actions;
    private ArrayList<ConditionObject> conditions;
    private boolean active = false;
    private int id = 0;
    private String contentString = null;
    private String dateString = null;

    public EventObject(String contentString, String dateString) {
        this.contentString = contentString;
        this.dateString = dateString;
    }

    public String getDescription() {
        return contentString;
    }
    public String getUpdateDate() {
        return dateString;
    }
    public static ActionObject createAction(int type, int id) {
        return ActionObject.createAction(type, id);
    }
    public static ConditionObject createCondition(int type, int id) {
        return ConditionObject.createCondition(type, id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContentString() {
        return contentString;
    }

    public void setContentString(String contentString) {
        this.contentString = contentString;
    }

    public String getDateString() {
        return dateString;
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
    }

    public ArrayList<ActionObject> getActions() {
        return actions;
    }

    public void addAction(ActionObject newAction) {
        if (newAction != null) {
            actions.add(newAction);
        }
    }

    public boolean isActive() {
        return active;
    }

    public void activate(boolean active) {
        this.active = active;
    }

    public ArrayList<ConditionObject> getConditions() {
        return conditions;
    }

    public void addCondition(ConditionObject newCondition) {
        if (newCondition != null) {
            conditions.add(newCondition);
        }
    }
    public boolean matchCondition(ConditionObject inputCondition) {
        boolean found = false;

        if (inputCondition != null) {
            for (ConditionObject existCondition : conditions) {
                if (existCondition.getType() == inputCondition.getType()
                        && existCondition.getCondition().equals(inputCondition.getCondition())) {
                    found = true;
                    break;
                }
            }
        }

        return found;
    }

    public void startWork() {
        activate(true);
        for (ActionObject singleAction : actions) {
            singleAction.perform();
        }
    }
}
